var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');

Page({

  data: {
		list: [],
		query: {
			pageNo: 1,
			pageSize: 10
		},
		hasMore: true
  },
	
  onLoad: function (options) {
		this.getHistory()
  },

  onReady: function () {

  },

  onShow: function () {
		
  },

  onHide: function () {

  },

  onUnload: function () {

  },
	
	onPullDownRefresh: function () {
		this.setData({
			'query.pageNo': 1,
			hasMore: true
		})
		this.getHistory()
	},
	
	onReachBottom: function () {
		if (this.data.hasMore) {
			let pageNo = this.data.query.pageNo + 1
			this.setData({
				'query.pageNo': pageNo
			})
			this.getHistory()
		}
	},
	
	getHistory() {
		let that = this
		util.request(api.GetGiftBalanceHistory, that.data.query).then(res => {
			if (res.errno == '0') {
				if (res.data.page != that.data.query.pageNo) {
					that.setData({
						hasMore: false
					})
					return
				}
				
				let arr = [...that.data.list, ...res.data.list]
				that.setData({
					list: arr
				})
			} else {
				that.setData({
					hasMore: false
				})
			}
			wx.stopPullDownRefresh()
		}).catch(err => {
			that.setData({
				hasMore: false
			})
		})
	}

})